﻿using System.Collections.Generic;
using System.Linq;
using Sumen.Business;
using Sumen.Library.Classes.Common;
using Sumen.Library.Enum;

using Sumen.Business.Data;
using Sumen.Library.Froms;
using Sumen.Business.Factory;
using Sumen.Business.Permission;
using Sumen.Business.Presenter.Sys;
using Sumen.Model.Model;



namespace Sumen.GUI.Sumen_System
{
    public partial class Frm_Permission_User : XtraUserControlKira
    {
        private Dao dao;
        private ICollection<user> _listUser;
        private string username;
        private List<sys_user_permission> listPermissions;


        public Frm_Permission_User()
        {
            InitializeComponent();
            dao = new Dao(false,true);
            FormCode = EnumFormCode.FrmPermissionUser;
            buttonsArray1.capQuyen(new Permission(FormCode.ToString(), ClsPublic.User.User_Username));
        }


        #region button Event

        private void buttonsArray1_btnEventUpdate_click(object sender, System.EventArgs e)
        {
            if (update())
            {
                ClsMsgBox.ThongTin("Cập nhật thành công");
                ClsPublic.WriteLog(FormCode, EnumFormStatus.Modify, username, "");
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, System.EventArgs e)
        {
            this.Dispose();
        }



        #endregion

        #region Method

        public override void LoadForm()
        {
            LoadTreelist();
            gridControl1.DataSource = _listUser = dao.GetAll<user>();
            base.LoadForm();

        }

        private void LoadTreelist()
        {
            var listModule = dao.GetAll<sys_object>();
            var listPermission = dao.FindByMultiColumnAnd<sys_user_permission>(new[] { "user" }, username);
            listPermissions = (from p in listModule
                               join p1 in listPermission on p.Syso_ID equals p1.UP_Object_ID into Inners
                               from i in Inners.DefaultIfEmpty(new sys_user_permission() { UP_Permission = 0 })
                               select new sys_user_permission()
                               {
                                   UP_Object_ID = p.Syso_ID,
                                   UP_User_ID = username,
                                   UP_Permission = i.UP_Permission,
                                   UP_Object_Parent_ID = p.Syso_Parent_ID,
                                   UP_Object_Name = p.Syso_Name
                               }).ToList();
            treeList1.DataSource = listPermissions;
            treeList1.ExpandAll();
            treeList1.BestFitColumns();
        }

        public override bool update()
        {
            EntityFactory.getInstance().BeginTransactionEntities();
            if (listPermissions != null && listPermissions.Count > 0)
            {
                if (dao.AddOrUpdate(listPermissions.ToArray()) <= 0)
                {
                    return false;
                }
                else
                {
                    EntityFactory.getInstance().commit();
                    return true;
                }
            }
            else
            {
                return false;
            }
            return base.update();
        }

        private void AutoCheckTreeList(string id, EnumFormStatus formStatus)
        {
            sys_user_permission userPermission = listPermissions.FirstOrDefault(p => p.UP_Object_ID == id);
            foreach (var sysUserPermission in listPermissions)
            {
                if (sysUserPermission.UP_Object_Parent_ID == id)
                {
                    if (formStatus == EnumFormStatus.View)
                        sysUserPermission.View = userPermission.View;
                    else if (formStatus == EnumFormStatus.Add)
                        sysUserPermission.Add = userPermission.Add;
                    else if (formStatus == EnumFormStatus.Modify)
                        sysUserPermission.Update = userPermission.Update;
                    else if (formStatus == EnumFormStatus.Delete)
                        sysUserPermission.Delete = userPermission.Delete;
                    else if (formStatus == EnumFormStatus.Report)
                        sysUserPermission.Report = userPermission.Report;
                    else if (formStatus == EnumFormStatus.Approved)
                        sysUserPermission.Approved = userPermission.Approved;
                    else if (formStatus == EnumFormStatus.Print)
                        sysUserPermission.Print = userPermission.Print;
                    else if (formStatus == EnumFormStatus.Excel)
                        sysUserPermission.Excel = userPermission.Excel;
                    else if (formStatus == EnumFormStatus.Word)
                        sysUserPermission.Word = userPermission.Word;
                    AutoCheckTreeList(sysUserPermission.UP_Object_ID, formStatus);
                }
            }
            treeList1.RefreshDataSource();
        }

        private void ChangeState(string id, EnumFormStatus formStatus)
        {
            sys_user_permission userPermission = listPermissions.FirstOrDefault(p => p.UP_Object_ID == id);
            if (formStatus == EnumFormStatus.View)
                userPermission.View = !userPermission.View;
            else if (formStatus == EnumFormStatus.Add)
                userPermission.Add = !userPermission.Add;
            else if (formStatus == EnumFormStatus.Modify)
                userPermission.Update = !userPermission.Update;
            else if (formStatus == EnumFormStatus.Delete)
                userPermission.Delete = !userPermission.Delete;
            else if (formStatus == EnumFormStatus.Report)
                userPermission.Report = !userPermission.Report;
            else if (formStatus == EnumFormStatus.Approved)
                userPermission.Approved = !userPermission.Approved;
            else if (formStatus == EnumFormStatus.Print)
                userPermission.Print = !userPermission.Print;
            else if (formStatus == EnumFormStatus.Excel)
                userPermission.Excel = !userPermission.Excel;
            else if (formStatus == EnumFormStatus.Word)
                userPermission.Word = !userPermission.Word;
        }

        #endregion

        #region TreeList event

        private void gridView1_DoubleClick(object sender, System.EventArgs e)
        {
            if (_listUser != null && _listUser.Count > 0)
            {

                username = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, colUsername);
                groupControl2.Text = "Quyền thao tác trên hệ thống - Tài khoản: " + username;
                LoadTreelist();
            }
        }

        private void resCheckView_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.View);
            AutoCheckTreeList(id, EnumFormStatus.View);
        }


        private void resCheckAdd_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Add);
            AutoCheckTreeList(id, EnumFormStatus.Add);
        }

        private void resCheckUpdate_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Modify);
            AutoCheckTreeList(id, EnumFormStatus.Modify);
        }

        private void resCheckDelete_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Delete);
            AutoCheckTreeList(id, EnumFormStatus.Delete);
        }

        private void resCheckPrint_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Print);
            AutoCheckTreeList(id, EnumFormStatus.Print);
        }

        private void resCheckWord_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Word);
            AutoCheckTreeList(id, EnumFormStatus.Word);
        }

        private void resCheckExcel_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Excel);
            AutoCheckTreeList(id, EnumFormStatus.Excel);
        }

        private void resCheckReport_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Report);
            AutoCheckTreeList(id, EnumFormStatus.Report);
        }

        private void resCheckApproved_CheckStateChanged(object sender, System.EventArgs e)
        {
            string id = treeList1.FocusedNode[treeList1.KeyFieldName].ToString();
            ChangeState(id, EnumFormStatus.Approved);
            AutoCheckTreeList(id, EnumFormStatus.Approved);
        }

        #endregion









    }
}